using com.haopintui.entity;
using System;
using System.IO;
using System.Text;
namespace com.haopintui.util
{
	public class ConfigUtil
	{
		public static void save_config(CmsForm cmsForm)
		{
			try
			{
				AppBean appBean = cmsForm.appBean;
				if (!Directory.Exists(cmsForm.app_path + "/config"))
				{
					Directory.CreateDirectory(cmsForm.app_path + "/config");
				}
				appBean.config_ini = cmsForm.app_path + Constants.config_ini;
				if (File.Exists(appBean.config_ini))
				{
					File.Delete(appBean.config_ini);
				}
				FileStream fileStream = new FileStream(appBean.config_ini, FileMode.Create);
				StreamWriter streamWriter = new StreamWriter(fileStream, Encoding.GetEncoding("GB2312"));
				streamWriter.WriteLine("autologin=" + (cmsForm.configUI.checkBoxAutoLogin.Checked ? "1" : "0"));
				streamWriter.WriteLine("alimamaacc=" + cmsForm.configUI.textBoxAlimamaAcc.Text.Trim());
				streamWriter.WriteLine("alimamapwd=" + cmsForm.configUI.textBoxAlimamaPwd.Text.Trim());
				streamWriter.WriteLine("uuWiseAcc=" + cmsForm.configUI.textBoxUUUsername.Text.Trim());
				streamWriter.WriteLine("uuWisePwd=" + cmsForm.configUI.textBoxUUPwd.Text.Trim());
				streamWriter.Flush();
				streamWriter.Close();
				streamWriter.Dispose();
				fileStream.Close();
				fileStream.Dispose();
				ConfigUtil.syn_config(cmsForm);
			}
			catch (Exception ex)
			{
				LogUtil.log_call(cmsForm, "[init_config]" + ex.ToString());
			}
		}
		public static void init_config(CmsForm cmsForm)
		{
			try
			{
				AppBean appBean = cmsForm.appBean;
				if (!Directory.Exists(cmsForm.app_path + "/config"))
				{
					Directory.CreateDirectory(cmsForm.app_path + "/config");
				}
				appBean.config_ini = cmsForm.app_path + Constants.config_ini;
				if (!File.Exists(appBean.config_ini))
				{
					FileStream fileStream = File.Create(appBean.config_ini);
					fileStream.Close();
					fileStream.Dispose();
				}
				StreamReader streamReader = new StreamReader(appBean.config_ini, Encoding.GetEncoding("GB2312"));
				string text;
				while ((text = streamReader.ReadLine()) != null)
				{
					appBean.hashtable_config.Add(text.Split(new char[]
					{
						'='
					})[0], text.Split(new char[]
					{
						'='
					})[1]);
				}
				streamReader.Close();
				streamReader.Dispose();
				cmsForm.configUI.textBoxAlimamaAcc.Text = (string)appBean.hashtable_config["alimamaacc"];
				cmsForm.configUI.textBoxAlimamaPwd.Text = (string)appBean.hashtable_config["alimamapwd"];
				cmsForm.configUI.checkBoxAutoLogin.Checked = "1".Equals((string)appBean.hashtable_config["autologin"]);
				cmsForm.configUI.textBoxUUUsername.Text = (string)appBean.hashtable_config["uuWiseAcc"];
				cmsForm.configUI.textBoxUUPwd.Text = (string)appBean.hashtable_config["uuWisePwd"];
				ConfigUtil.syn_config(cmsForm);
			}
			catch (Exception ex)
			{
				LogUtil.log_call(cmsForm, "[init_config]" + ex.ToString());
			}
		}
		public static void syn_config(CmsForm cmsForm)
		{
			cmsForm.appBean.alimama_acc = cmsForm.configUI.textBoxAlimamaAcc.Text.Trim();
			cmsForm.appBean.alimama_pwd = cmsForm.configUI.textBoxAlimamaPwd.Text.Trim();
			cmsForm.appBean.alimama_auto_login = cmsForm.configUI.checkBoxAutoLogin.Checked;
			cmsForm.appBean.uu_username = cmsForm.configUI.textBoxUUUsername.Text.Trim();
			cmsForm.appBean.uu_pwd = cmsForm.configUI.textBoxUUPwd.Text.Trim();
		}
	}
}
